package com.business.user.service.impl;

import com.business.common.enums.BusinessExceptionEnum;
import com.business.common.enums.ErrorEnum;
import com.business.common.exception.BusinessException;
import com.business.common.exception.CustomException;
import com.business.common.utils.RandomHelper;
import com.business.common.utils.StrKit;
import com.business.user.config.SmsConfig;
import com.business.user.mapper.UserMapper;
import com.business.user.service.SmsService;
import com.business.user.utils.SmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SmsServiceImpl implements SmsService {

    private static final Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);

    private SmsConfig smsConfig;

    @Autowired
    private UserMapper userMapper;

    /**
     * 发送
     *
     * @param phone
     * @return
     */
    @Override
    public Boolean send(String phone, String username, String dateTime, String templateCode) {
        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        if (StrKit.isNotEmpty(dateTime)) {
            map.put("time", dateTime);
        }
        boolean flag = SmsUtil.sendSms(phone, map, smsConfig.getTemplates().get(templateCode));
        if (!flag) {
            logger.error("发送失败,phone:" + phone + ",,参数:" + map);
            throw new CustomException(ErrorEnum.UNKNOWN_ERROR);
        }
        return flag;
    }


    @Override
    public Boolean sendToUser(int userId, String dateTime, String templateCode) {
        //获取用户手机和名称
        Map<String, String> userInfoMap = userMapper.getNameAndPhone(userId);
        if (userInfoMap == null || userInfoMap.get("name") == null) {
            throw new BusinessException(BusinessExceptionEnum.USER_NON_EXIST);
        } else if (userInfoMap.get("phone") == null) {
            throw new BusinessException(BusinessExceptionEnum.PHONE_NULL);
        }
        String phone = userInfoMap.get("phone");
        String userName = userInfoMap.get("name");
        return send(phone, userName, dateTime, templateCode);
    }


    /**
     * 发送认证码
     *
     * @param phone
     * @return
     */
    @Override
    public String sendVerificationCode(String phone) {
        String code = RandomHelper.numberic(4) + "";
        boolean flag = SmsUtil.sendSms(phone, code, smsConfig.getTemplates().get("verificationCode"));
        if (!flag) {
            logger.error("发送失败,phone:" + phone + ",code:" + code);
            //如果发送失败默认验证码为8888
            throw new CustomException(ErrorEnum.UNKNOWN_ERROR, "8888");
        }
        return code;
    }

    @Autowired
    public void setSmsConfig(SmsConfig smsConfig) {
        this.smsConfig = smsConfig;
        SmsUtil.setSignName(smsConfig.getSignName());
        SmsUtil.setAccessKeyId(smsConfig.getAccessKeyId());
        SmsUtil.setAccessKeySecret(smsConfig.getAccessKeySecret());
    }
}
